﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Management;


namespace GameXNA_Master
{
    class OptionMenu : Menu
    {
        public enum voice { Risoluzione, SchermoIntero, Input, Volume_Effetti, Volume_Musica, Indietro };

        public int nrResolutionvoices = 0;
        public string[] voiceText = new string[] { "RISOLUZIONE", "SCHERMO INTERO", "COMANDI", "VOLUME EFFETTI", "VOLUME MUSICA", "INDIETRO" };
        public string[] schermoInteroVoice = new string[] { "SI", "NO" };
        public int nrVoices = Enum.GetNames(typeof(voice)).Length;


        int selectedVoice = 0;
        public Vector2 starPosition;

        public int selRes;
        public bool FullScreen = false;
        public bool useJoystic = false;

        SettingsData settingsdata;
        Game game;
        InputService input_service;
        RenderingData rendering_data;
        float rotateStar = 0;

        SpriteFont menufont;
        Texture2D selectionTexture;
        Texture2D option;

        public event Action iMoveUp_Menu;
        public event Action iMoveDown_Menu;

        public event Action iMoveLeft_Menu;
        public event Action iMoveRight_Menu;
        public event Action iConfirm_Menu;

        public OptionMenu(Game game)
        {

            this.game = game;
            this.input_service = (InputService)game.Services.GetService(typeof(InputService));
            this.rendering_data = (RenderingData)game.Services.GetService(typeof(RenderingData));
            this.settingsdata = (SettingsData)game.Services.GetService(typeof(SettingsData));
            this.selectionTexture = game.Content.Load<Texture2D>(@"Menu\star");
            this.menufont = game.Content.Load<SpriteFont>("matrix");
            this.option = game.Content.Load<Texture2D>(@"Menu\space_invaders_wallpaper_option");
            selRes = settingsdata.selectedResolution;
            useJoystic = settingsdata.useJoystic;

            iMoveUp_Menu += () =>
            {

                if (selectedVoice == 0)

                    selectedVoice = nrVoices - 1;
                else
                {
                    selectedVoice--;
                }

            };

            iMoveDown_Menu += () =>
            {

                if (selectedVoice == nrVoices - 1)

                    selectedVoice = 0;
                else
                {
                    selectedVoice++;
                }

            };

            iMoveLeft_Menu += () =>
            {
                switch (selectedVoice)
                {
                    case (int)voice.Risoluzione:

                        selRes = (selRes == 0) ? nrResolutionvoices - 1 : --selRes; // devo vedere in renderibng data x nrResolutionvoices
                        selRes %= nrResolutionvoices;

                        break;

                    case (int)voice.SchermoIntero:

                        FullScreen = !FullScreen;
                        break;

                    case (int)voice.Input:

                        useJoystic = (!useJoystic && GamePad.GetState(PlayerIndex.One).IsConnected);
                        BulletLogic.useJoystick = useJoystic;
                        break;

                    case (int)voice.Volume_Effetti:

                        settingsdata.effectVolume = MathHelper.Clamp(settingsdata.effectVolume - 0.01f, 0, 1);
                        break;

                    case (int)voice.Volume_Musica:

                        settingsdata.musicVolume = MathHelper.Clamp(settingsdata.musicVolume - 0.01f, 0, 1);
                        break;

                    default: break;
                }
            };

            iMoveRight_Menu += () =>
            {
                switch (selectedVoice)
                {
                    case (int)voice.Risoluzione:

                        selRes = (selRes == nrResolutionvoices - 1) ? 0 : ++selRes;
                        selRes %= nrResolutionvoices;

                        break;

                    case (int)voice.SchermoIntero:

                        FullScreen = !FullScreen;
                        break;

                    case (int)voice.Input:

                        useJoystic = (!useJoystic && GamePad.GetState(PlayerIndex.One).IsConnected);
                        BulletLogic.useJoystick = useJoystic;
                        break;

                    case (int)voice.Volume_Effetti:

                        settingsdata.effectVolume = MathHelper.Clamp(settingsdata.effectVolume + 0.01f, 0, 1);
                        break;

                    case (int)voice.Volume_Musica:

                        settingsdata.musicVolume = MathHelper.Clamp(settingsdata.musicVolume + 0.01f, 0, 1);
                        break;
                    default: break;
                }
            };

            iConfirm_Menu += () =>
            {

                SavegameHandler save = new SavegameHandler();

                save.saveSettings(settingsdata, rendering_data, this);

                input_service.Save_Game_Settings();


                rendering_data.aspectRatio = game.GraphicsDevice.Viewport.AspectRatio;

                menu_selezionato = 0;
            };

        }



        public void MoveUp()
        {
            if (iMoveUp_Menu != null)
                iMoveUp_Menu();
        }

        public void MoveDown()
        {
            if (iMoveDown_Menu != null)
                iMoveDown_Menu();
        }

        public void MoveLeft()
        {
            if (iMoveLeft_Menu != null)
                iMoveLeft_Menu();
        }
        public void MoveRight()
        {
            if (iMoveRight_Menu != null)
                iMoveRight_Menu();
        }

        public void ConfirmAction()
        {
            if (iConfirm_Menu != null)
                iConfirm_Menu();
        }

        int menu_selezionato = 1;

        public void Update(GameTime gameTime, out int SelectedMenu)
        {
            rotateStar += (float)gameTime.ElapsedGameTime.TotalSeconds * 50;

            nrResolutionvoices = rendering_data.screenHeightResolutions.Count;
            SelectedMenu = menu_selezionato;
            menu_selezionato = 1;

            switch (selectedVoice)
            {
                case 0:
                    {
                        starPosition = Vector2.Lerp(starPosition, new Vector2(16, 17), (float)gameTime.ElapsedGameTime.TotalSeconds * 5f);
                        break;
                    }
                case 1:
                    {
                        starPosition = Vector2.Lerp(starPosition, new Vector2(16, 32), (float)gameTime.ElapsedGameTime.TotalSeconds * 5f);

                        break;
                    }
                case 2:
                    {
                        starPosition = Vector2.Lerp(starPosition, new Vector2(16, 48), (float)gameTime.ElapsedGameTime.TotalSeconds * 5f);
                        break;
                    }
                case 3:
                    {
                        starPosition = Vector2.Lerp(starPosition, new Vector2(16, 63), (float)gameTime.ElapsedGameTime.TotalSeconds * 5f);
                        break;
                    }
                case 4:
                    {
                        starPosition = Vector2.Lerp(starPosition, new Vector2(16, 78), (float)gameTime.ElapsedGameTime.TotalSeconds * 5f);
                        break;
                    }

                case 5:
                    {
                        starPosition = Vector2.Lerp(starPosition, new Vector2(76, 92), (float)gameTime.ElapsedGameTime.TotalSeconds * 5f);
                        break;
                    }

                default: break;
            }





        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, null, Matrix.CreateScale(rendering_data.screenWidth / 100.0f, rendering_data.screenHeight / 100.0f, 1.0f));

            spriteBatch.Draw(option, new Rectangle(0, 0, 100, 100), Color.White);

            spriteBatch.DrawString(menufont, voiceText[0] + ((selectedVoice == (int)OptionMenu.voice.Risoluzione) ? "\n  << " + rendering_data.screenWidthtResolutions[selRes] + " x " + rendering_data.screenHeightResolutions[selRes]
                + " >>" : "\n     " + rendering_data.screenWidthtResolutions[selRes] + " x " + rendering_data.screenHeightResolutions[selRes])
                , new Vector2(20, 15), (selectedVoice == (int)OptionMenu.voice.Risoluzione) ? Color.Orange : Color.White, 0, Vector2.Zero, 0.08f, SpriteEffects.None, 0f);

            string fs = (FullScreen) ? "SI" : "NO";
            spriteBatch.DrawString(menufont, voiceText[1] + ((selectedVoice == (int)OptionMenu.voice.SchermoIntero) ? "\n\t << " + fs + " >>" : "\n\t    " + fs)
                , new Vector2(20, 30), (selectedVoice == (int)OptionMenu.voice.SchermoIntero) ? Color.Orange : Color.White, 0, Vector2.Zero, 0.08f, SpriteEffects.None, 0f);

            string typeInput = (useJoystic) ? "GAMEPAD" : "TASTIERA/MOUSE";
            string infoGamepad = (GamePad.GetState(PlayerIndex.One).IsConnected) ? " -! GamePad Connesso !-" : " -! GamePad Assente !-";
            spriteBatch.DrawString(menufont, voiceText[2] + infoGamepad + ((selectedVoice == (int)OptionMenu.voice.Input) ? "\n  << " + typeInput + " >>" : "\n  " + typeInput)
                , new Vector2(20, 45), (selectedVoice == (int)OptionMenu.voice.Input) ? Color.Orange : Color.White, 0, Vector2.Zero, 0.08f, SpriteEffects.None, 0f);



            spriteBatch.DrawString(menufont, voiceText[3] + ((selectedVoice == (int)OptionMenu.voice.Volume_Effetti) ? "\n  << " + (int)(100 * settingsdata.effectVolume) + " >>" : "\n  " + (int)(100 * settingsdata.effectVolume))
                , new Vector2(20, 60), (selectedVoice == (int)OptionMenu.voice.Volume_Effetti) ? Color.Orange : Color.White, 0, Vector2.Zero, 0.08f, SpriteEffects.None, 0f);

            spriteBatch.DrawString(menufont, voiceText[4] + ((selectedVoice == (int)OptionMenu.voice.Volume_Musica) ? "\n  << " + (int)(100 * settingsdata.musicVolume) + " >>" : "\n  " + (int)(100 * settingsdata.musicVolume))
                , new Vector2(20, 75), (selectedVoice == (int)OptionMenu.voice.Volume_Musica) ? Color.Orange : Color.White, 0, Vector2.Zero, 0.08f, SpriteEffects.None, 0f);




            spriteBatch.DrawString(menufont, voiceText[5], new Vector2(80, 90), (selectedVoice == (int)OptionMenu.voice.Indietro) ? Color.Orange : Color.White, 0, Vector2.Zero, 0.07f, SpriteEffects.None, 0f);


            spriteBatch.Draw(selectionTexture, starPosition, null, Color.White, MathHelper.ToRadians(rotateStar), new Vector2(selectionTexture.Width / 2, selectionTexture.Height / 2), 0.01f, SpriteEffects.None, 0.0f);


            spriteBatch.End();
        }
    }
}
